package com.lkz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkz.entity.Role;
import com.lkz.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  前端控制器
 */
@RestController
@RequestMapping("/emp")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @PostMapping(value = "/role/uploadExcl")
    @ResponseBody
    public  Map<String ,Object> uploadExcl(HttpServletRequest request, @RequestParam("file") MultipartFile file){
        Map<String ,Object> result = new HashMap<>();
        String path = request.getSession().getServletContext().getRealPath("/");
        try{
            // 如果文件不为空，写入上传路径
            if(!file.isEmpty()){
                result = roleService.uploadExcl(file);
            }else {
                result.put("code","1");
                result.put("message","上传文件为空！");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
    @GetMapping("/role")
    public List<Role> selectRole(){
        return roleService.list();
    }
    @GetMapping("/role/list")
    public Map getList(Integer page, Integer limit){
        Map<String,Object> map = new HashMap<String,Object>();
        Page<Role> pages = new Page<Role>();
        pages.setSize(limit);
        pages.setCurrent(page);
        IPage<Role> iPage = roleService.selectList(pages);
        map.put("msg","");
        map.put("count",iPage.getTotal());
        map.put("data",iPage.getRecords());
        map.put("code",0);
        return map;
    }
    @GetMapping("/role/selectByDel")
    public Map selectByDel(Integer page, Integer limit){
        Map<String,Object> map = new HashMap<String,Object>();
        Page<Role> pages = new Page<Role>();
        pages.setSize(limit);
        pages.setCurrent(page);
        IPage<Role> iPage = roleService.selectByDel(pages);
        map.put("msg","");
        map.put("count",iPage.getTotal());
        map.put("data",iPage.getRecords());
        map.put("code",0);
        return map;
    }
    @PostMapping("/role")
    public Map add(Role role,Integer deptId,Integer permId){
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("state",roleService.add(role,deptId,permId));
        return result;
    }
    @PutMapping("/role")
    public Map edit(Role role,Integer deptId,Integer permId){
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("state",roleService.update(role,deptId,permId));
        return result;
    }
    @DeleteMapping("/role/del/{roleId}")
    public Map del(@PathVariable Integer roleId){
        Map<String,Object> result = new HashMap<String,Object>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_id",roleId);
        Role role = new Role();
        role.setIsDel(1);
        result.put("state",roleService.update(role,queryWrapper));
        return result;
    }
    @DeleteMapping("/role/recover/{roleId}")
    public Map recover(@PathVariable Integer roleId){
        Map<String,Object> result = new HashMap<String,Object>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_id",roleId);
        Role role = new Role();
        role.setIsDel(0);
        result.put("state",roleService.update(role,queryWrapper));
        return result;
    }
}
